package com.start.boot.web;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.start.boot.adquery.AdQueryRequest;
import com.start.boot.adquery.AdvanceQueryResult;
import com.start.boot.common.MessageResult;
import com.start.boot.pojo.vo.AdvanceQueryVo;
import com.start.boot.service.AdvanceQueryService;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 高级查询控制器
 *
 * @caomin
 * @create 2017-12-13 11:43
 **/
@RestController
@RequestMapping("/advanceQuery")
public class AdvanceQueryController {



    @Autowired
    AdvanceQueryService advanceQueryService;



    @Autowired
    AdvanceQueryResult advanceQueryResult;



    @ApiOperation("获取所有初始化组件")
    @GetMapping("/getAllCondition")
    @org.springframework.cache.annotation.Cacheable("getAllCondition")
    public MessageResult getAllCondition()throws Exception{
        List<AdvanceQueryVo> allCondition = advanceQueryService.getAllCondition();
        return new MessageResult("获取成功",200,allCondition);
    }

    @ApiOperation("获取单个组件值")
    @GetMapping("/getSingleCondition")
    @org.springframework.cache.annotation.Cacheable("getSingleCondition")
    public MessageResult getSingleCondition(String bh)throws Exception{
        Object value= advanceQueryService.getSingleCondition(bh);
        return new MessageResult("获取成功",200,value);
    }


    @ApiOperation("获取查询结果")
    @PostMapping("/getResult")
    public  MessageResult getResult(@RequestBody AdQueryRequest request) throws Exception {
        String sql = advanceQueryService.startQuery(request);
        if (StringUtils.isNotEmpty(sql)){
            PageHelper.startPage(request.getPage(), request.getPageSize());
            List<Map> reuslt = advanceQueryService.getReuslt(sql);
            PageInfo page = new PageInfo(reuslt);
            return   new MessageResult("获取成功",200,reuslt, (int) page.getTotal());
        }
        return new MessageResult("获取成功",200,null);
    }




}
